package com.xqq.lottery.protocal;

import java.io.StringWriter;

import org.xmlpull.v1.XmlSerializer;

import android.util.Log;
import android.util.Xml;

import com.xqq.lottery.ConstantValue;

public class Message {
	
	private Header header =new Header();
	
	private Body body = new Body();
	
	public void serializerMessage(XmlSerializer serializer){
		try{
			serializer.startTag(null, "message");
			serializer.attribute(null, "version", "1.0");
			header.serializerHeader(serializer, body.getBodyContent());
			body.serializerBody(serializer);
			serializer.endTag(null, "message");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public String getXml(Element element){
		if(element == null){
			throw new IllegalArgumentException("element is null");
		}
		header.getTransactiontype().setTagValue(element.getTransactionType());
		
		body.getElements().add(element);
		XmlSerializer serializer = Xml.newSerializer();
		StringWriter writer =new StringWriter();
		try{
			serializer.setOutput(writer);
			serializer.startDocument(ConstantValue.ENCONDING, null);
			serializerMessage(serializer);
			serializer.endDocument();
			return writer.toString();
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public Header getHeader() {
		return header;
	}

	public Body getBody() {
		return body;
	}

}
